.section .init
start:
	ldr pc, _resetHandler
	ldr pc, _undefinedHandler
	ldr pc, _swiHandler
	ldr pc, _prefetchAbortHandler
	ldr pc, _dataAbortHandler
	ldr pc, _unusedSpot
	ldr pc, _irqHandler
	ldr pc, _fiqHandler
;@
;@ Définition des handlers
;@
_resetHandler: .word reset
_undefinedHandler: .word hang
_swiHandler: .word swi_handler
_prefetchAbortHandler: .word hang
_dataAbortHandler: .word hang
_unusedSpot: .word hang
_irqHandler: .word hang
_fiqHandler: .word hang
;@
;@ Reset
;@
reset:
	;@ On créer la vector table
	mov r0, #0x8000
	mov r1, #0x0000
	ldmia r0!, {r2, r3, r4, r5, r6, r7, r8, r9}
	stmia r1!, {r2, r3, r4, r5, r6, r7, r8, r9}
	ldmia r0!, {r2, r3, r4, r5, r6, r7, r8, r9}
    stmia r1!, {r2, r3, r4, r5, r6, r7, r8, r9}
	;@
	;@ On alloue la pile pour le mode FIQ
	mov r0, #0xD1 ;@ Mode_FIQ , IRQ_DISABLE , FIQ_DISABLE
	msr cpsr_c, r0
	mov sp, #0x4000
	;@
	;@ On alloue la pile pour le mode RIQ
	mov r0, #0xD2 ;@ Mode_RIQ , IRQ_DISABLE , FIQ_DISABLE
	msr cpsr_c, r0
	mov sp, #0x8000
	;@
	;@ On alloue la pile pour le superviseur
	mov r0, #0xD3 ;@ Mode_SPV , IRQ_DISABLE , FIQ_DISABLE
	msr cpsr_c, r0
	mov sp, #0x8000000
	;@ On passe à la suite, en C
	mov r0, #0xD3 ;@ Mode_SPV , IRQ_DISABLE , FIQ_DISABLE
	msr cpsr_c, r0
	bl kmain
;@
;@ Ne fait rien, reste bloqué
;@
hang:
	b hang
;@
;@ Géstionnaire de SWI
swi_handler:
	push {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
	bl SWIHandler
	pop  {r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, lr}
	movs pc, r14
;@
;@ Permet d'activer les interruptions IRQ
;@ En C : void EnableIRQ(void)
.globl EnableIRQ
EnableIRQ:
	mrs r0, cpsr
	bic r0, r0, #0x80
	msr cpsr_c, r0
	bx lr
;@
;@ Lance une SWI
.globl ThrowSWI
ThrowSWI:
	swi 0
	bx lr
